﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SQLCEExample.Model;
using Microsoft.Xna.Framework.GamerServices;

namespace SQLCEExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        public const string ConnectionString = @"isostore:/MainDataContext.sdf"; //ConnectionString of CE database
        public City city = null; //create Instance of City class 

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            using (MainDataContext maindc = new MainDataContext(ConnectionString))
            {
                if (!maindc.DatabaseExists())
                {
                    maindc.CreateDatabase();
                }
            }

            InsertRecords();

            GetRecords();
        }

        /// <summary>
        /// This Method create for Insert Records in city table of database
        /// </summary>
        public void InsertRecords()
        {
            using (MainDataContext mainDataContext = new MainDataContext(ConnectionString))
            {
                City city = new City();
                city.Name = "Indore";
                city.CountryCode = "452001";
                mainDataContext.Cities.InsertOnSubmit(city);

                city = new City();
                city.Name = "Jaora";
                city.CountryCode = "457226";
                mainDataContext.Cities.InsertOnSubmit(city);

                city = new City();
                city.Name = "Badawada";
                city.CountryCode = "457227";
                mainDataContext.Cities.InsertOnSubmit(city);

                mainDataContext.SubmitChanges();
            }
        }

        /// <summary>
        /// This method create for get total records of city table from database
        /// </summary>
        public void GetRecords()
        {
            using (MainDataContext mainDataContext = new MainDataContext(ConnectionString))
            {
                var data = from d in mainDataContext.Cities select d;
                lstdata.ItemsSource = null; //Before Insert record first set as null
                lstdata.ItemsSource = data.ToList(); // Assign city list to lstdata ListBox
                lstdata.UpdateLayout();
            }
        }

        /// <summary>
        /// Selection change event of lstdata listbox
        /// </summary>
        /// <param name="sender">ListBox control</param>
        /// <param name="e">ListBox change event</param>
        private void lstdata_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                city = e.AddedItems[0] as City;
                // ConfirmationBox
                Guide.BeginShowMessageBox("ConfrimBox", "Are you want to delete this record?", new string[] { "Yes", "No" }, 1, MessageBoxIcon.None,
                                                    new AsyncCallback(OnMessageBoxClosed), null);
            }
        }

        /// <summary>
        /// Close fo custom message box
        /// </summary>
        /// <param name="ar">Asyncr Result of confirmationbox</param>
        private void OnMessageBoxClosed(IAsyncResult ar)
        {
            try
            {
                int? buttonIndex = Guide.EndShowMessageBox(ar);

                if (buttonIndex == 0)
                {
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        using (MainDataContext mainDataContext = new MainDataContext(ConnectionString))
                        {
                            var data = from d in mainDataContext.Cities select d;
                            var newdata = data.ToList().First(x => x.Name == city.Name);
                            mainDataContext.Cities.DeleteOnSubmit(newdata);
                            mainDataContext.SubmitChanges();
                        }

                        //Aftre delete refresh the list
                        GetRecords();
                    });
                }
            }
            catch (Exception)
            { }
        }
    }
}